package com.epam.cdp.selenium2.core.configuration;

import java.util.ArrayList;
import java.util.List;

import com.epam.cdp.selenium2.core.common.SpecialSymbols;
import com.epam.cdp.selenium2.core.exception.ItemNotFoundException;
import com.epam.cdp.selenium2.core.runner.options.Option;
import com.epam.cdp.selenium2.core.runner.options.Parameter;

/**
 * Configuration witch contains user options. Options place to configuration during parsing XML file.
 * 
 * @author yyudzitski
 */
public class OptionsConfiguration
{
  private List<Option> options;

  public OptionsConfiguration()
  {
    options = new ArrayList<Option>();
  }

  public Option getOptionByName( String optionName )
  {
    for( Option option : options )
    {
      if( option.getOptionName().equals( optionName ) )
      {
        return option;
      }
    }
    throw new ItemNotFoundException( "Option " + SpecialSymbols.SINGLE_QUOTE.toString() + optionName
      + SpecialSymbols.SINGLE_QUOTE.toString() + " not found in configuration" );
  }

  public String getOptionParameterValue( String optionName, String parameterName )
  {
    Option option = getOptionByName( optionName );
    for( Parameter parameter : option.getOptionParameters() )
    {
      if( parameter.getName().equals( parameterName ) )
      {
        return parameter.getValue();
      }
    }
    throw new ItemNotFoundException( "Parameter " + SpecialSymbols.SINGLE_QUOTE.toString() + parameterName
      + SpecialSymbols.SINGLE_QUOTE.toString() + " not exists for option " + SpecialSymbols.SINGLE_QUOTE.toString()
      + optionName + SpecialSymbols.SINGLE_QUOTE.toString() );
  }

  /**
   * Add option to configuration if it not exists
   * 
   * @param option
   */
  public void addOption( Option option )
  {
    if( !options.contains( option ) )
    {
      options.add( option );
    }
  }
}